// AWTTextField.cpp: implementation of the AWTTextField class.
//
// 17/8/2000 MF - Helpdesk ref: 27357 - 2 char text field box not wide
//				  enough e.g. entered GB showed 1 char and then scrolled.
// 4/8/2000  MF - Helpdesk ref: 27917 - fix from john re: multi question
//				  1 char accepted 2 chars.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "ctl3d.h"
//#include "AWTTest.h"
#include "AWTTextField.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTTextField, AWTTextComponent)

AWTTextField::AWTTextField()
{
	construct(CString(""), 0);
}

AWTTextField::AWTTextField(LPTSTR text)
{
	construct(CString(text), lstrlen(text));
}

AWTTextField::AWTTextField(CString& text)
{
	construct(text, text.GetLength());
}

AWTTextField::AWTTextField(int columns)
{
	construct(CString(""), columns);
}

AWTTextField::~AWTTextField()
{
	if (m_actionListener != NULL)
	{
		if (m_actionListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_actionListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_actionListener);
#endif
			delete mc;
		}
	}
}

CWnd* AWTTextField::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	CEdit* pwnd = new AWTTextFieldPeer(this);
//	DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER;
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL;
	if (m_upperCase)
		dwStyle |= ES_UPPERCASE;
	if (echoCharIsSet())
		dwStyle |= ES_PASSWORD;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);

	if (m_border)
		pwnd->ModifyStyleEx(0, WS_EX_CLIENTEDGE);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetMargins(1,1);
	if (m_columns > 0)
		pwnd->SetLimitText(m_columns);
	pwnd->SetReadOnly(m_readOnly);

	if (!m_enabled)
		pwnd->EnableWindow(FALSE);

	pwnd->SetWindowText(m_text);
	return pwnd;
}

void AWTTextField::setUpperCase(BOOL b)
{
	m_upperCase = b;
	if (getPeer() != NULL)
	{
		if (b)		
			getPeer()->ModifyStyle(0, ES_UPPERCASE);
		else
			getPeer()->ModifyStyle(ES_UPPERCASE, 0);
	}
}

void AWTTextField::setReadOnly(BOOL b)
{
	m_readOnly = b;
	if (getPeer() != NULL)
		((CEdit*)getPeer())->SetReadOnly(b);
}

void AWTTextField::setEchoChar(TCHAR ch)
{
	m_echoChar = ch;
	if (echoCharIsSet())
	{
		if (getPeer() != NULL)
			getPeer()->ModifyStyle(0, ES_PASSWORD);
	}
}
																														 
CSize AWTTextField::preferredSize(int columns) 
{
//	if (m_peer == NULL)
//		return CSize(0,0);
	CSize sz(0,0);
//	CEdit* pwnd = (CEdit*)m_peer;
	HDC hdc = ::GetDC(NULL);
	CDC* pdc = CDC::FromHandle(hdc);
	CFont* ft = CFont::FromHandle(getFont());
	CFont* ftOld = pdc->SelectObject(ft);

	TCHAR c[256];
	for (int n = 0; n < columns; n++)
		c[n] = 'X';
//	memset(c, 'X', columns);
	c[columns] = '\0';
	GetTextExtentPoint32(pdc->m_hDC, c, columns, &sz);
	sz.cx += 6;
	sz.cy += 6;
	pdc->SelectObject(ftOld);
	::ReleaseDC(NULL, hdc);	
/*
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
//	sz.cx = (tm.tmMaxCharWidth * columns) + 6;
	sz.cx = ((tm.tmAveCharWidth + tm.tmOverhang) * columns) + columns;
	sz.cy = tm.tmHeight + 6;
*/
	return sz;
}

CSize AWTTextField::preferredSize() 
{
//	if (m_peer == NULL)
//		return CSize(0,0);
	return preferredSize(m_columns);
}

CSize AWTTextField::minimumSize(int columns) 
{
//	if (m_peer == NULL)
//		return CSize(0,0);
	CSize sz(0,0);
	CEdit* pwnd = (CEdit*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 6;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize AWTTextField::minimumSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return minimumSize(m_columns);
}

void AWTTextField::setColumns(int columns)
{
	if (columns < 0) return;
	if (m_columns != columns)
	{
		m_columns = columns;
		invalidate();
		if (getPeer() != NULL)
		{
			((CEdit*)getPeer())->SetLimitText(m_columns);
		}
	}
}
/*
void AWTTextField::setText(CString& text)
{
	m_text = text;
	if (m_peer != NULL)
	{	
//		m_peer->PostMessage(WM_AWTSETTEXT, 0, (LPARAM)new CString(text));
		m_peer->SetWindowText(m_text);	
	}
}

CString& AWTTextField::getText()
{
	if (m_peer != NULL)
		m_peer->GetWindowText(m_text);
	return m_text;
}
*/
